import store from '@/store'
import { Toast } from 'vant'
import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [{
        path: '/',
        name: '/',
        redirect: '/main/menu'
    },
    {
        path: '/main',
        name: 'main',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/Main.vue'),
        children: [{
                path: 'menu',
                name: 'menu',
                component: () =>
                    import ('../views/Menu.vue'),
                meta: { index: 1 }
            },
            {
                path: 'order',
                name: 'order',
                component: () =>
                    import ('../views/Order.vue'),
                meta: { index: 2, hasLogin: true }
            }, {
                path: 'category',
                name: 'category',
                component: () =>
                    import ('../views/Category.vue'),
                meta: { index: 3 }
            },
            {
                path: 'user',
                name: 'user',
                component: () =>
                    import ('../views/User.vue'),
                meta: { index: 4 }
            }
        ]
    }, {
        path: '/searchFood',
        name: 'SearchFood',
        component: () =>
            import ('../views/SearchFood.vue'),
        meta: { index: 5 }
    },
    {
        path: '/foodDetail',
        name: 'FoodDetail',
        component: () =>
            import ('../views/FoodDetail.vue'),
        meta: { index: 6 }
    },
    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ('../views/Login.vue'),
        meta: { index: 7 }
    },
    {
        path: '/register',
        name: 'Register',
        component: () =>
            import ('../views/Register.vue'),
        meta: { index: 8 }
    },
    {
        path: '/shopCart',
        name: 'ShopCart',
        component: () =>
            import ('../views/ShopCart.vue'),
        meta: { index: 9, hasLogin: true }
    },
    // {
    //     path: '/addAddressInfo',
    //     name: 'AddAddressInfo',
    //     component: () =>
    //         import ('../views/AddAddressInfo.vue'),
    //     meta: { index: 10, hasLogin: true }
    // },
    // {
    //     path: '/editAddressInfo',
    //     name: 'EditAddressInfo',
    //     component: () =>
    //         import ('../views/EditAddressInfo.vue'),
    //     meta: { index: 10, hasLogin: true }
    // }
    {
        path: '/setting',
        name: 'Setting',
        component: () =>
            import ('../views/Setting.vue'),
        meta: { index: 11, hasLogin: true }
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

//前置路由守卫
router.beforeEach((to, from) => {
    if (to.meta.hasLogin === true) {
        if (!store.state.userInfo) {
            Toast('请登录')
            return to.path = { path: '/login', query: { redirect: to.fullPath } }
        }
    }
})

export default router